package shortcircuit.engine;

import java.awt.Dimension;
import java.util.ArrayList;

import shortcircuit.GameEngine;
import shortcircuit.Display;
import shortcircuit.basics.Cell;
import shortcircuit.basics.Environment;
import shortcircuit.basics.EnvironmentPane;
import shortcircuit.graphics.EndGameScreen;
import shortcircuit.graphics.GameScreen;
import shortcircuit.graphics.MainMenuScreen;
import shortcircuit.graphics.StatsScreen;
import shortcircuit.utilities.FileIO;

/**
 * This class is a subclass of the more generalized version, GameEngine.
 * The puzzle engine runs all of the behind-the-scenes acts of Endless
 * Mode in this project.<p>
 * Functions include the definitions of the start and end of the game.
 * 
 * @author Kevin Lewi
 */
public class EndlessEngine extends GameEngine
{
	int[] restrictedBlocks = {1};
	/**
	 * Initiates the endless engine by calling the game engine's
	 * constructor.
	 * 
	 * @author Kevin Lewi
	 */

	public EndlessEngine(Display theDisplay)
	{
		super(theDisplay, 1);
		
		queuedBlocks.add(createRandomBlock(restrictedBlocks));
		queuedBlocks.add(createRandomBlock(restrictedBlocks));
		queuedBlocks.add(createRandomBlock(restrictedBlocks));
		_Time = 0.0;
		beginGame();
	}


	/**
	 * Runs the GamePulse counting thread when the game initiates.
	 * 
	 * @author Kevin Lewi
	 */
	void beginGame()
	{
		Environment env = new Environment(new Dimension(5, 10));
		EnvironmentPane ep = new EnvironmentPane(env);
		super.createMap(ep);
		_Display.clearScreens();
		gameScreen = new GameScreen(this);
		statsScreen = new StatsScreen(this);
		_Display.directlyAddScreen(popUpScreen, 0);
		_Display.addScreen(gameScreen);
		_Display.addScreen(statsScreen);
		_Display.directlyAddScreen(MainMenuScreen.bgPanel, 3);
	}

	public void update()
	{
		if(popUpScreen.doneLoading)
		{
			gameScreen.addListeners();
			_Display.removeScreen(popUpScreen);
			_Time++;
			if(_Time % (int)(150/Math.pow(1.1,_Level)) == 0 && allCompletePaths.size() == 0)
				generateRandomBlock(restrictedBlocks);
		}
		super.update();
	}

	/**
	 * Stops the GamePulse counting thread when the game ends.
	 * 
	 * @author Kevin Lewi
	 */
	public void checkEndGame()
	{
		for(int x = 0; x < _Environment.getGrid().length; x++)
		{
			if(_Environment.getCell(x, _Environment.getGrid()[x].length - 1).hasBlock())
			{
				gamePulse.setInterrupted(true);
				gameScreen.removeListeners();
				gameScreen.setIgnoreRepaint(true);
				popUpScreen = new EndGameScreen(1, false, this);
				_Display.directlyAddScreen(popUpScreen, 0);
			}
		}
	}


}
